package com.myboot.demo.controller;

import com.myboot.demo.config.ProjectUrl;
import com.myboot.demo.constant.CookieConstant;
import com.myboot.demo.constant.RedisConstant;
import com.myboot.demo.entity.SellerInfo;
import com.myboot.demo.enums.ResultEnum;
import com.myboot.demo.service.SellerService;
import com.myboot.demo.utils.CookieUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Author: 韦溜溜
 * @Description:
 * @Date: Create in 14:19 2019/1/12
 */
@Controller
@Slf4j
@RequestMapping("/seller")
public class SellerUserController {

    @Autowired
    private SellerService sellerService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ProjectUrl projectUrl;

    @GetMapping("/login")
    public ModelAndView login(@RequestParam("openid") String openid,
                              Map<String,Object>map,
                              HttpServletResponse response){

        //openid跟数据库进行比对
        SellerInfo sellerInfo = sellerService.findSellerInfoByopenid(openid);
        if(sellerInfo == null){
            map.put("msg", ResultEnum.LOGIN_FAIL.getMessage());
            map.put("url","/sell/seller/order/list");
            return new ModelAndView("common/error",map);
        }
        //设置token至redis
        String token = UUID.randomUUID().toString();//通用唯一识别码
        Integer expire = RedisConstant.EXPIRE;
        redisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN_PREFIX,token),openid,expire, TimeUnit.SECONDS);
        //设置token至token
        CookieUtil.setCookie(response, CookieConstant.TOKEN,token,expire);

        return new ModelAndView("redirect:"+projectUrl.getSell()+"/sell/seller/order/list");
        //重定向最好是把地址全部写出来
    }

    @GetMapping("/logout")
    public ModelAndView logout(HttpServletResponse response,
                               HttpServletRequest request,
                               Map<String,Object> map ){
        //通过cookie查询
        Cookie cookie = CookieUtil.get(request,CookieConstant.TOKEN);
        //清除redis
        redisTemplate.opsForValue().getOperations().delete(String.format(RedisConstant.TOKEN_PREFIX,cookie.getValue()));
        //清除cookie
        CookieUtil.setCookie(response,CookieConstant.TOKEN,null,0);
        map.put("msg",ResultEnum.LOGOUT_SUCCESS.getMessage());
        map.put("url","/sell/seller/order/list");
        return new ModelAndView("/common/success",map);
    }
}
